package com.huabo.contract.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.FtpUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.contract.oracle.entity.TblAttachment;
import com.huabo.contract.oracle.entity.TblContractProject;
import com.huabo.contract.oracle.mapper.TblAttachmentMapper;
import com.huabo.contract.oracle.mapper.TblContractProjectMapper;
import com.huabo.contract.oracle.mapper.TblLegalDisputregistrationMapper;
import com.huabo.contract.oracle.vo.TblContractProjectVo;
import com.huabo.contract.service.TblContractProjectService;

@Service
public class TblContractProjectServiceImpl implements TblContractProjectService {
	@Autowired
	private TblContractProjectMapper tblContractProjectMapper;
	
	@Resource
	private TblLegalDisputregistrationMapper tblLegalDisputregistrationMapper;
	
	@Resource
	private TblAttachmentMapper tblAttachmentMapper;
	
	
//  @Autowired
//  private ActivityPluginsService activityPluginsService;

	@Override
	public JsonBean cpPageList(String token, Integer pageNumber, Integer pageSize,
			TblContractProjectVo tblContractProjectVo) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Integer staffid = loginStaff.getStaffid().intValue();
		
    	if(pageNumber == null) {
    		pageNumber = 1;
    	}
    	if(pageSize==null) {
    		pageSize=15;
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>(0);
    	
    	PageInfo<TblContractProject> pageInfo = new PageInfo<TblContractProject>();
//    	tblNbsjWorkReport.setAuditorgid(loginStaff.getCurrentOrg().getOrgid());
//    	pageInfo.setCondition(tblNbsjWorkReport);
    	pageInfo.setPageSize(pageSize);
    	pageInfo.setCurrentPage(pageNumber);
    	pageInfo.setTlist(this.tblContractProjectMapper.selectListByPageInfo(pageInfo,tblContractProjectVo,staffid));
    	pageInfo.setTotalRecord(this.tblContractProjectMapper.selectCountByPageInfo(pageInfo,tblContractProjectVo,staffid));
    	pageInfo.getTotalPage();
//    	String identifier = activityPluginsService.getoNState(ProcessEnum.SJ_JHGL.name());
//    	resultMap.put("identifier", identifier);
    	resultMap.put("pageInfo", pageInfo);
    	return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean cpAdd(TblContractProject cp, String token, String attIds) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		
		Integer count = this.tblContractProjectMapper.selectPlanCodeByOrgid(cp);
		if(count > 0) {
			return ResponseFormat.retParam(0,202,null);
		}
    	
		cp.setCreatestaffid(loginStaff.getStaffid()+"");
		cp.setCreatetime(new Date());
		cp.setSTATE("0");
		//根据planId主键是否为空判断新增还是修改 ，主键为空新增、不为空修改；
		
		if(cp.getProjectid() != null) {
			//修改；
			this.tblContractProjectMapper.updateEntity(cp);
		}else {
			//新增；
			this.tblContractProjectMapper.insertEntity(cp);
		}
		if(attIds != null && !"".equals(attIds)) {
			String[] attId = attIds.split(",");
			for (String id : attId) {
				tblLegalDisputregistrationMapper.saveAttacheMent(5, cp.getProjectid(),new BigDecimal(id));
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		resultMap.put("cp",cp);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean cpDelete(Integer projectid, String token) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		TblContractProject plan = this.tblContractProjectMapper.selectById(projectid);
		
		if(plan == null) {
			return ResponseFormat.retParam(0,50001,null);
		}
		
//		if (plan.getOpinionstatus().equals(TblNbsjAuditplan.SPNO)) {
//			this.tblContractProjectMapper.deleteAuditPlanEntityById(planId);
//			return ResponseFormat.retParam(0,200,null);
//        } else {
//            return ResponseFormat.retParam(0,50001,null);
//        }
		this.tblContractProjectMapper.deleteFileRelation(projectid);
		this.tblContractProjectMapper.deleteById(projectid);
		return ResponseFormat.retParam(1,200,null);
	}

	@Override
	public JsonBean findContractProjectDetail(String token, Integer projectid) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
		Map<String,Object> resultMap = new HashMap<String,Object>(0);
		
		TblContractProject plan = this.tblContractProjectMapper.selectById(projectid);
		resultMap.put("cp", plan);
		return ResponseFormat.retParam(1,200,resultMap);
	}

	@Override
	public JsonBean removeFile(String token, String attId) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if(loginStaff == null) {
			return ResponseFormat.retParam(0,20006,null);
		}
    	
		TblAttachment tblAttachmentEntity = this.tblAttachmentMapper.findById(attId);
        if (tblAttachmentEntity == null) {
        	return ResponseFormat.retParam(0,203,null);
        }
        FtpUtil.removeFile(tblAttachmentEntity.getAttpath());
        this.tblContractProjectMapper.deleteFileId(attId);
        this.tblAttachmentMapper.deleteAtt(attId);
        return ResponseFormat.retParam(1,200,null);
	}
}
